<!--
 * @Author: 张久冬
 * @Date: 2021-12-29 11:37:34
 * @
# Description:
-->
<template>
  <div class="page-search">
    <JDForm v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <span>高级检索</span>
      </template>
      <template #footer>
        <div class="handleBtns">
          <el-button @click="resetClick"
            ><el-icon><refresh /></el-icon>
            重置
          </el-button>
          <el-button type="primary" @click="queryClick"
            ><el-icon><search /></el-icon>
            查询
          </el-button>
        </div>
      </template>
    </JDForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, watch, ref } from 'vue'
import JDForm from '@/base-ui/form/index'

export default defineComponent({
  emits: ['reserBtnClick', 'queryBtnClick'],
  props: {
    searchFormConfig: {
      type: Object,
      required: true
    }
  },
  components: { JDForm },
  setup(props, { emit }) {
    // 双向绑定的数据应该来源于searchFormConfig中定义的field
    const formItems = props.searchFormConfig?.formItems ?? []
    const formOriginData: any = {}
    formItems.map((item) => {
      formOriginData[item.field] = ''
    })
    const formData = ref(formOriginData)
    watch(
      formData,
      (newValue) => {
        console.log('page-content组件内部绑定数据', newValue)
      },
      {
        deep: true
      }
    )
    // 重置按钮
    const resetClick = () => {
      formData.value = formOriginData
      emit('reserBtnClick')
    }
    // 搜索按钮
    const queryClick = () => {
      emit('queryBtnClick', formData.value)
    }
    return { formData, resetClick, queryClick }
  }
})
</script>

<style scoped>
.footer {
  text-align: right;
}
.handleBtns {
  text-align: right;
  padding: 15px;
}
</style>
